<?php

/**
 * @name: HOTN Scroller
 * @author: HammerOnTheNail
 * @link: http://code.google.com/p/hotnscroller/
 */

$eplug_admin = TRUE;
include("../../../class2.php"); 

$sql = new db;

// Admin jog vizsgálata
if(!getperms("P"))
{ 
	header("location:".e_BASE."index.php"); 
}

if ($_POST['action'] == "add")
{
	$count = $sql->db_Count("hotn_scroller_ittems");
	
	require_once(e_HANDLER."upload_handler.php");
	$uploaded = process_uploaded_files(e_PLUGIN."hotn_scroller/images", "unique", NULL);
	
	$add = array(
		"ittems_title" => $_POST['title'],
		"ittems_text" => $_POST['text'],
		"ittems_link" => $_POST['link'],
		"ittems_link_type" => $_POST['link_type'],
		"ittems_order" => ($count+1),
		"ittems_image" => $uploaded['0']['name']
	);
	
	$sql->db_Insert("hotn_scroller_ittems",$add);
	$action = "?ittem.add.save";
}

if ($_GET['action'] == "ittem_up")
{
	
	$id = (int)$_GET['id'];
	$sql->db_Select("hotn_scroller_ittems","*","ittems_id = ".$id,"default");
	$ittem = $sql->db_Fetch();
	$ittem = $ittem['ittems_order']-1;

	if ($ittem > 0)
	{
		$sql->db_Update("hotn_scroller_ittems","ittems_order = ittems_order + 1 WHERE ittems_order = ".$ittem);
		$sql->db_Update("hotn_scroller_ittems","ittems_order = ittems_order - 1 WHERE ittems_id = ".$id);
	}
	$action = "?ittem.list.order";
}

if ($_GET['action'] =="ittem_down")
{
	$id = (int)$_GET['id'];
	$sql->db_Select("hotn_scroller_ittems","*","ittems_id = ".$id,"default");
	$ittem = $sql->db_Fetch();
	$ittem = $ittem['ittems_order']+1;
	
	if ($ittem < $sql->db_Count("hotn_scroller_ittems"))
	{
		$sql->db_Update("hotn_scroller_ittems","ittems_order = ittems_order - 1 WHERE ittems_order = ".$ittem);
		$sql->db_Update("hotn_scroller_ittems","ittems_order = ittems_order + 1 WHERE ittems_id = ".$id);
	}
	$action = "?ittem.list.order";
}

if ($_POST['action'] == "delete")
{
	$action = "?ittem.list.delete";
	
	$arg = "`ittems_id` = '".implode($_POST['error'],"' or `ittems_id` = '")."'";
	
	if ($sql->db_Delete("hotn_scroller_ittems",$arg) === 0)
	{
		$action = "?ittem.list.no_select";	
	}	
}

if ($_POST['action'] == "edit")
{
	$id = (int)$_POST['id'];
	$sql->db_Select("hotn_scroller_image","*","ittems_id = ".$id);
	$image = $sql->db_Fetch();
	$update['ittems_image'] = FALSE;
	if ($_POST['delete_file_userfile'] == TRUE)
	{
		@unlink(e_PLUGIN."hotn_scroller/images/".$image['ittems_image']);
		$update['ittems_image'] = "";
	}
	
	if ($_FILES['file_userfile']['error'] == "0")
	{
		require_once(e_HANDLER."upload_handler.php");
		$uploaded = process_uploaded_files(e_PLUGIN."hotn_scroller/images", "unique", NULL);
		$update['ittems_image'] = $uploaded[0]['name'];
	}

	if ($update['ittems_image'] !== FALSE) $update = "`ittems_image` = '".$update['ittems_image']."', ";
	else $update = "";
	
	$update .= "`ittems_title`='".$_POST['title']."', ";
	$update .= "`ittems_text`='".$_POST['text']."', ";
	$update .= "`ittems_link`='".$_POST['link']."', ";
	$update .= "`ittems_link_type`='".$_POST['link_type']."'";
	
	$sql->db_Update("hotn_scroller_ittems",$update." WHERE `ittems_id` = '".$id."'");
	
	$action = "?ittem.list.edit";
}

if ($_POST['options'] == "edit")
{
	$sql->db_Delete("hotn_scroller_options");
	foreach ($_POST as $name => $value)
	{
		if ($name <> "options" )
		{
			$instert = array(
				"hotn_scroller_options_name" => $name,
				"hotn_scroller_options_value" => $value
			);
			$sql->db_Insert("hotn_scroller_options",$instert);
		}
	}
	$action = "?options.edit.save";
}

header("Location: ".e_PLUGIN."hotn_scroller/admin/admin_index.php".$action);
?>